package com.yjc.drygoods.cart.controller;

import com.yjc.common.utils.R;
import com.yjc.drygoods.cart.service.CartService;
import com.yjc.drygoods.cart.vo.Cart;
import com.yjc.drygoods.cart.vo.CartItem;
import com.yjc.drygoods.cart.vo.CartItemChecked;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @author yjc
 * @create 2022-09-21 19:08
 */
@Slf4j
@Controller
public class CartController {

    @Autowired
    CartService cartService;

    /**
     * 批量更改购物项选中状态
     *
     * @param cartItemCheckeds
     * @return
     */
    @ResponseBody
    @PostMapping("/checkAllCartItem")
    public R checkAllCartItem(@RequestBody List<CartItemChecked> cartItemCheckeds) {
//        cartItemCheckeds.forEach(System.out::println);
        for (CartItemChecked cartItemChecked : cartItemCheckeds) {
            cartService.setCartItemCheckStatus(cartItemChecked.getSkuId(), cartItemChecked.getCheck());
        }
        return R.ok();
    }

    @GetMapping("/delete/allCartItems")
    public String deleteAllCartItems() {
        cartService.deleteAllCartItem();
        return "redirect:http://cart.drygoods.top/cartList.html";
    }

    @ResponseBody
    @GetMapping("/get/currentUser/allCartItems")
    public R getCurrentUserAllCartItems() {
        List<CartItem> cartItems = cartService.getCurrentUserAllCartItems();
        return R.ok().setData(cartItems);
    }

    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId,
                            @RequestParam("check") Integer check) {
        cartService.setCartItemCheckStatus(skuId, check);
        return "redirect:http://cart.drygoods.top/cartList.html";

    }

    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId") Long skuId,
                            @RequestParam("num") Integer num) {
        cartService.setCartItemCount(skuId, num);
        return "redirect:http://cart.drygoods.top/cartList.html";

    }

    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("deleteId") Long deleteId) {
        cartService.deleteCartItem(deleteId);
        return "redirect:http://cart.drygoods.top/cartList.html";

    }

    /**
     * 删除选中部分的购物项
     *
     * @param deleteIds
     * @return
     */
    @ResponseBody
    @PostMapping("/deleteItem/selectPart")
    public R deleteItemSelectPart(@RequestBody List<Long> deleteIds) {
        deleteIds.forEach(System.out::println);
        for (Long deleteId : deleteIds) {
            cartService.deleteCartItem(deleteId);
        }
        return R.ok();

    }

    @GetMapping("/cartList.html")
    public String cartPage(Model model) throws ExecutionException, InterruptedException {

        Cart cart = cartService.getCart();
        model.addAttribute("cart", cart);
        return "cartList";
    }

    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId,
                            @RequestParam("num") Integer num,
                            RedirectAttributes redirectAttributes) throws ExecutionException, InterruptedException {

        CartItem cartItem = cartService.addToCart(skuId, num);
        redirectAttributes.addAttribute("skuId", skuId);
        return "redirect:http://cart.drygoods.top/addToCart.html";
    }

    @GetMapping("/addToCart.html")
    public String addToCartHtml(@RequestParam("skuId") Long skuId,
                                Model model) throws ExecutionException, InterruptedException {

        CartItem cartItem = cartService.getCartItem(skuId);
        model.addAttribute("cartItem", cartItem);
        return "success";
    }
}
